var fs = require('fs');
var mkdirp = require('mkdirp');
async = require("async");

var wxml = require('./wxmlConvert');
var wxss = require('./wxssConvert');
var jsConvert = require('./jsConvert');
var fileHepler = require('./fileHepler');
var geneMainJs = require('./generatorMain');

function fileMerge(projectPath, distPath){

    var appPath = projectPath+"\\app.json";
    //得到app.json里面的配置
    var appJson=JSON.parse(fs.readFileSync( appPath) );
    //路由和页面路径
    var paths = appJson.pages;

    for(var i=0; i<paths.length; i++){
        //利用闭包解决i值不对
        (function(i){
        var vueContent = '';
        //1获取template
        var wxmlContent = wxml.getContent(projectPath+'/'+paths[i]);
        vueContent = '<template>\n'+wxmlContent + '\n</template>\n';

        //2获取config
        var configContent = fs.readFileSync( projectPath+'/'+paths[i]+'.json');
        vueContent += '\n<script type="text/babel">\n' 
        + '         export default {\n'
        + '         config: {\n'
        + '             window:'
        + configContent + '\n'
        + '  },\n'

        //3获取
        var pageContent = jsConvert.getContent(projectPath+'/'+paths[i]);
        vueContent += pageContent
        + '        }\n'
        +'</script>';

        //2、3拼凑完整的export default 

        //4获取css
        var wxssContent = wxss.getContent(projectPath+'/'+paths[i]);
        // console.log(wxssContent);
        vueContent += '\n\n <style scoped>\n'+ wxssContent + '\n</style>\n';
        var dirTmp = distPath+'/'+paths[i];
        dirTmp = dirTmp.substring(0,dirTmp.lastIndexOf('/'))
        //利用三方模块穿件文件夹
        mkdirp(dirTmp, function (err) { 
            var _path = distPath+'/'+paths[i]+'.vue';
            if (err) {
                console.error(err) 
            }
            else{
                console.log('pow!')
                fs.writeFile(_path, vueContent,function(err){
                    if(err) {
                        console.log(err);
                    }
                    else {
                        console.log(_path +' convert finished ');
                    }
                });
            } 
        });
    })(i)
    }

    setTimeout(() => {
        geneMainJs.geneMainJs(distPath, paths)
    }, 3000);
}

create = function(wxPath, distPath){
var convertFlag = false;
//同步操作,必须同步否则拷贝文件不全
async.series([
function(callback){//先清空文件夹
    fileHepler.deleteFolderRecursive(distPath);
    callback(null, 1);
}, 
function(callback){//创建临时转换的文件夹
    fs.mkdir(distPath, 0777, function(err){
        if(err){
         console.log(err);
        }else{
         console.log("remove others success!");
        }
       })
       callback(null, 2);
    },
    function(callback) {//将模板工程拷贝到临时目录
        fileHepler.copyFolder('./project', distPath, null);
        callback(null, 3);
    }, 
    function(callback) {//将微信代码合并后的文件拷贝到临时目录
        fileMerge(wxPath, distPath+'/src');
        callback(null, 4);
    }
    ]);
}

create("D:\\xpackage\\ui\\wx\\dist", 'D:\\test');

exports.create = create;